import { Module } from '@nestjs/common';
import { TasksModule } from './business/tasks/tasks.module';
import { TypeOrmModule } from '@nestjs/typeorm';
import { AuthModule } from './system/auth/auth.module';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { configVaildationSchema } from './config/config.schema';
import { EventsModule } from './system/socket/socket.module';
import { ThemeModule } from './system/theme/theme.module';
import { LogModule } from './system/log/log.module';
import { DbLogger } from './system/utils/log4js/log4js';
import { FileModule } from './system/file/file.module';
import { RoleModule } from './system/role/role.module';
import { RolesGuard } from './system/auth/roles.guard';
import { APP_GUARD } from '@nestjs/core';
import { MenuModule } from './system/menu/menu.module';
import Crypto from './system/utils/secret/secret';

@Module({
  imports: [
    ConfigModule.forRoot({
      envFilePath: [`.env.stage.${process.env.STAGE}`],
      validationSchema: configVaildationSchema,
    }),
    TypeOrmModule.forRootAsync({
      imports: [ConfigModule],
      inject: [ConfigService],
      // eslint-disable-next-line @typescript-eslint/ban-ts-comment
      // @ts-ignore
      useFactory: async (configService: ConfigService) => {
        const isProduction =
          configService.get('STAGE') === 'prod' ||
          process.env.NODE_ENV === 'production';
        return {
          //   ssl: isProduction,
          //   extra: {
          //     ssl: isProduction ? { rejectUnauthorized: false } : null,
          //   },
          type: configService.get('DB_TYPE'),
          host: configService.get('DB_HOST'),
          port: configService.get('DB_PORT'),
          username: configService.get('DB_USER_NAME'),
          password: configService.get('DB_USER_PASSWORD'),
          database: configService.get('DB_NAME'),
          autoLoadEntities: true,
          synchronize: true,
          logging: [configService.get('DB_LOGGING')],
          logger: new DbLogger(), // 配置项添加自定义的log类
          maxQueryExecutionTime: 1500,
          timezone: '+08:00',
        };
      },
    }),
    TasksModule,
    AuthModule,
    EventsModule,
    ThemeModule,
    LogModule,
    FileModule,
    RoleModule,
    MenuModule,
  ],
  providers: [
    {
      provide: APP_GUARD,
      useClass: RolesGuard,
    },
    Crypto,
  ],
})
export class AppModule {}
